#region license
/*
 * Copyright (C) 2007 CchenSoft.com
 * 
 * See http://www.cchensoft.com/opensource/cc-portal/license.txt
 * 
 * Author: Billy Zhang
 * Email: billy_zh@126.com
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Web.UI.WebControls;
using CchenSoft.Framework.Utils;

namespace CchenSoft.Portal.Web
{
    [ParseChildren(true)]
    public abstract class ChildControl : Control, INamingContainer
    {
        protected override void CreateChildControls()
        {
            ReflectUtil.InjectBeans(this);

            Control control = this.LoadSkin();
            this.Initialize(control);
            this.Controls.Add(control);
        }

        private void Initialize(Control control)
        {
            try
            {
                InitializeSkin(control);
            }
            catch (Exception ex)
            {
                Error(control, ex);
            }
        }

        protected abstract void InitializeSkin(Control control);

        protected virtual void Error(Control control, Exception ex)
        {
            //control.Controls.Clear();

            Literal lt = new Literal();
            lt.Text = ex.ToString().Replace(Environment.NewLine, "<br/>");
            control.Controls.Add(lt);
        }

        protected abstract Control LoadSkin();
    }
}
